﻿#include "columnchart.h"
#include <QDebug>

ColumnChart::ColumnChart(QWidget* parent)
{
	y_max = 100;
	width_column = 30;
	float_mode = FloatMode::FloatUp;
	float_animation = new QPropertyAnimation(this);
	float_animation->setTargetObject(this);
	float_animation->setDuration(500);
	float_animation->setStartValue(0.0);
	float_animation->setEndValue(1.0);
	float_rate = 0;
	connect(float_animation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value)
		{
			float_rate = value.toDouble();
			update();
		});
	connect(float_animation, &QPropertyAnimation::finished, this, [=]
		{
			if (float_mode == FloatMode::FloatUp)
				float_mode = FloatMode::FloatDown;
			else if (float_mode == FloatMode::FloatDown)
				float_mode = FloatMode::FloatLeft;
			else if (float_mode == FloatMode::FloatLeft)
				float_mode = FloatMode::FloatRight;
			else if (float_mode == FloatMode::FloatRight)
				float_mode = FloatMode::FloatUp;
		});
}

void ColumnChart::addData(const QString& desc, double value, const QColor& color)
{
	chart_value[desc] = value;
	chart_color[desc] = color;
}

void ColumnChart::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	int start_x = (width() - chart_width) / 2;
	int start_y = (height() - chart_height) / 2;
	//绘制坐标轴
	painter.drawLine(QPoint(start_x, start_y + chart_height), QPoint(start_x, start_y));
	painter.drawLine(QPoint(start_x, start_y + chart_height), QPoint(start_x + chart_width, start_y + chart_height));
	painter.drawLine(QPoint(start_x, start_y), QPoint(start_x - 6, start_y + 8));
	painter.drawLine(QPoint(start_x, start_y), QPoint(start_x + 6, start_y + 8));
	painter.drawLine(QPoint(start_x + chart_width, start_y + chart_height), QPoint(start_x + chart_width - 8, start_y + chart_height + 6));
	painter.drawLine(QPoint(start_x + chart_width, start_y + chart_height), QPoint(start_x + chart_width - 8, start_y + chart_height - 6));
	//n个统计条会分割出n+1个空白区域，计算空白区域大小
	int width_white = (chart_width - chart_value.size() * width_column) / (chart_value.size() + 1);
	int width_cover = 0;
	for (auto key : chart_value.keys())
	{
		if (float_rate == 0)
			break;
		//先预留空白
		width_cover += width_white;
		painter.setBrush(QBrush(chart_color[key]));
		if (float_mode == FloatMode::FloatDown)
			painter.drawRect(start_x + width_cover, (start_y + chart_height * (1 - chart_value[key] / y_max)) * float_rate, width_column, chart_height * chart_value[key] / y_max * float_rate);
		if (float_mode == FloatMode::FloatUp)
			painter.drawRect(start_x + width_cover, start_y + chart_height - (chart_height * chart_value[key] / y_max * float_rate), width_column, chart_height * chart_value[key] / y_max * float_rate);
		if (float_mode == FloatMode::FloatRight)
			painter.drawRect(start_x + width_cover, start_y + chart_height * (1 - chart_value[key] / y_max), width_column * float_rate, chart_height * chart_value[key] / y_max);
		if (float_mode == FloatMode::FloatLeft)
			painter.drawRect(start_x + width_cover + width_column * (1 - float_rate), start_y + chart_height * (1 - chart_value[key] / y_max), width_column * float_rate, chart_height * chart_value[key] / y_max);
		width_cover += width_column;
	}
}

void ColumnChart::resizeEvent(QResizeEvent* event)
{
	chart_width = width() * 0.8;
	chart_height = height() * 0.8;
}

void ColumnChart::mousePressEvent(QMouseEvent* event)
{
	float_animation->start();
}
